<?php
/**
 * Created by PhpStorm.
 * User: qttx
 * Date: 2019/4/15
 * Time: 15:24
 */

namespace app\api\controller;

use think\Controller;
use think\Db;

class Message extends Controller
{
    //获取accesstoken

    public function getAccessToken($appid, $secret)
    {

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";

        $res = $this->curl_get($url);

        $res = json_decode($res, 1);

        return $res['access_token'];

    }

//获取模板消息内容主体

//因为是测试所以写死,大家可以通过传参的方式获取

    public function dianzan($openid,$name,$uid,$number,$title)
    {
        $appid = 'wx6c1a86ee3e43e4a3';//小程序appid
        $openid = $openid;//接收用户的openid
        $template_id = 'F-OJFLhV9KQUk57D3Kd07cfTD_4EWntZFk8VeSPBIXI';//从微信后台获取的模板id
        //file_put_contents('a.php',$formid);
        $time = time();
        $formid=Db('formid')->where('uid',$uid)->where('endtime','gt',$time )->field('formid')->find();
        $form_id = $formid['formid'];//七天内的formid
        $res=Db('formid')->where('formid',$form_id)->delete();
        $secret = '32f54b415ab46d898b2714c327b8d4b9';//密钥
        $data = $this->send($appid, $secret, $openid, $template_id, $form_id,$title,$number ,$name );
        return $data;//打印测试结果
    }
    public function send($appid, $secret, $openid, $template_id, $form_id, $name, $phone, $content)
    {
        $access_token = $this->getAccessToken($appid, $secret);
        $send_url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $access_token;
        $data = $this->getMsg( $openid, $template_id, $form_id, $name, $phone, $content);
        //return $data;
        $str = $this->curl_post($send_url, json_encode($data));
        $str = json_decode($str, 1);
        return $str;
    }
    public function getMsg($openid, $template_id, $form_id, $name, $phone, $content, $emphasis_keyword = 'keyword1')
    {
        $time = date('Y-m-d', time());

        $data['data'] = [
            'keyword1' => ['value' => $name, 'color' => ''],
            'keyword2' => ['value' => $phone, 'color' => ''],
            'keyword3' => ['value' => $content, 'color' => ''],
            'keyword4' => ['value' => $time, 'color' => ''],
        ];//内容主体
        $data['touser'] = $openid;//用户的openid

        $data['template_id'] = $template_id;//从微信后台获取的模板id

        $data['form_id'] = $form_id;//前端提供给后端的form_id

        $data['page'] = 'pages/yemian/tongzhi/tongzhi';//小程序跳转页面

        $data['emphasis_keyword'] = $emphasis_keyword;//选择放大的字体
        return $data;

    }



    public function curl_post($url, $fields, $data_type = 'text')

    {

        $cl = curl_init();

        if (stripos($url, 'https://') !== FALSE) {

            curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);

            curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);

            curl_setopt($cl, CURLOPT_SSLVERSION, 1);

        }

        curl_setopt($cl, CURLOPT_URL, $url);

        curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($cl, CURLOPT_POST, true);

        curl_setopt($cl, CURLOPT_POSTFIELDS, $fields);

        $content = curl_exec($cl);

        $status = curl_getinfo($cl);

        curl_close($cl);

        if (isset($status['http_code']) && $status['http_code'] == 200) {

            if ($data_type == 'json') {

                $content = json_decode($content);

            }

            return $content;

        } else {

            return FALSE;

        }

    }

    public function curl_get($url, $data_type = 'text')

    {

        $cl = curl_init();

        if (stripos($url, 'https://') !== FALSE) {

            curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);

            curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);

            curl_setopt($cl, CURLOPT_SSLVERSION, 1);

        }

        curl_setopt($cl, CURLOPT_URL, $url);

        curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);

        $content = curl_exec($cl);

        $status = curl_getinfo($cl);

        curl_close($cl);

        if (isset($status['http_code']) && $status['http_code'] == 200) {

            if ($data_type == 'json') {

                $content = json_decode($content);

            }

            return $content;

        } else {

            return FALSE;

        }

    }

    public function index($openid, $name, $phone, $content,$uid)
    {

        $appid = 'wx6c1a86ee3e43e4a3';//小程序appid

        $openid = $openid;//接收用户的openid

        $template_id = 'fNvaDpQqnOYWLc10i6S_lmUzMyZddwGgSXTIzU0GNWE';//从微信后台获取的模板id
		//file_put_contents('a.php',$formid);
      	$time = time();
      	$formid=Db('formid')->where('uid',$uid)->where('endtime','gt',$time )->field('formid')->find();
        $form_id = $formid['formid'];//七天内的formid\
		$res=Db('formid')->where('formid',$form_id)->delete();
        $secret = '32f54b415ab46d898b2714c327b8d4b9';//密钥
        $data = $this->send($appid, $secret, $openid, $template_id, $form_id, $name, $phone, $content);
        return $data;//打印测试结果

    }




    public function ceshi($openid, $name, $phone, $content, $formid)
    {
        $time=date('Y年m月d日 H:i:s',time());
        // POST请求配置数据
        $vpush_api = "https://wx6c1a86ee3e43e4a3.mssnn.cn/v2/api/vpush?id=3";
        $vpush_data = array(
            "secret" => "5ea04-55bc1-fecd1-4d1be",
            "path" => "pages/yemian/tongzhi/tongzhi",
            "openId" => $openid,
            //"data" =>
            "data"=>[
                $name,
                $phone,
                $time,
               $content
            ]);
        $vpush_header = array(
            "Content-Type: application/json"
        );

        // 使用curl进行POST请求
        $vpush_curl = curl_init();
        curl_setopt($vpush_curl, CURLOPT_POST, 1);
        curl_setopt($vpush_curl, CURLOPT_HEADER, 0);
        curl_setopt($vpush_curl, CURLOPT_RETURNTRANSFER, 0);

        curl_setopt($vpush_curl, CURLOPT_URL, $vpush_api);
        curl_setopt($vpush_curl, CURLOPT_HTTPHEADER, $vpush_header);
        curl_setopt($vpush_curl, CURLOPT_POSTFIELDS, json_encode($vpush_data));


        $vpush_result = curl_exec($vpush_curl);
        curl_close($vpush_curl);

        // 处理返回的结果
        //print_r($vpush_result);
        return $vpush_result;
    }
}